

#Call requires here
#Requires to classes, modules, controllers, routers, and so. 

require 'View'
require 'Model'

class Control 

  def initialize(vw,model)
    @view = vw
    @mod = model
  end

  def request(env)
    html = @view.getCurrentHTML()
    [200, {"Content-Type" => "text/html"}, html]
    #[200, {"Content-Type" => "text/plain"}, 'html']
  end 
  
  def action(env)
    #p env["QUERY_STRING"]
    input = env["QUERY_STRING"][5..-13]
    @view.startElement("center")
    @view.addItem("p",input)
    listData = @mod.collectData(input)
    hashTotals = @mod.dataProcess(listData)
    #listHashesRegions = @mod.getRegionVotes(listData) 
    candidateNames = hashTotals.keys
    @view.insertTable(hashTotals,candidateNames,["Candidato", "Total"])
    @view.closeElement
    @view.makeshiftGraph(hashTotals,candidateNames)
    @view.addItem("br","")
    #@view.massInsertTable(listHashesRegions,candidateNames)
    
    [200, {"Content-Type" => "text/html"},@view.getCurrentHTML()]
  end
  
  #constructor for Control class
  #notify method, aka CONTROLLER, manages routing after input
  def notify(env)
    #URI fetching
    #p env["PATH_INFO"]
    path = env["PATH_INFO"][1..-1]
    
    case
      when(path == "")
        return [200, {"Content-Type" => "text/plain"}, "Here goes Docspoller main page"]
      when (self.respond_to? path)
        self.send(path,env)
      else
        return [404, {"Content-Type" => "text/plain"}, "classic 404"]
    end
    
  end
  

end
